package com.android.JiYclocks;

import java.text.DateFormatSymbols;
import java.util.Calendar;

import com.android.JiYclock.R;

import android.content.Context;
import android.database.Cursor;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.BaseColumns;

public final class SpecialAlarm implements Parcelable {

    //////////////////////////////
    // 继承Parcelable接口，利用Parcel完成序列号，方便Alarm对象中的值在各个Activity中传递
    //////////////////////////////
	
    public static final Parcelable.Creator<SpecialAlarm> CREATOR
            = new Parcelable.Creator<SpecialAlarm>() {
    	//通过Parcel获得一个Alarm对象
                public SpecialAlarm createFromParcel(Parcel p) {
                    return new SpecialAlarm(p);
                }

                public SpecialAlarm[] newArray(int size) {
                    return new SpecialAlarm[size];
                }
            };

    public int describeContents() {
        return 0;
    }
        
    //把数据写入到Parcel中去
    public void writeToParcel(Parcel p, int flags) {
    	
        p.writeInt(id);
        p.writeInt(enabled ? 1 : 0);
        p.writeInt(year);
        p.writeInt(month);
        p.writeInt(day);
        p.writeInt(hour);
        p.writeInt(minutes);
        p.writeLong(time);
        p.writeInt(vibrate ? 1 : 0);
        p.writeString(label);
        p.writeParcelable(alert, flags);
        p.writeInt(silent ? 1 : 0);
        p.writeInt(times);
        p.writeInt(interval);
        //记录地址
        p.writeString(location);
    }
    //////////////////////////////
    // end Parcelable apis
    //////////////////////////////

    //////////////////////////////
    // Column definitions
    //////////////////////////////
    
    
    
    //BaseColumns是一个借口，里面有String型变量_id和_Count
    
    public static final String AUTHORITY = "com.android.superdeskclock.provider.specialalarm";
    
    public static class Columns implements BaseColumns {
        /**
         * The content:// style URL for this table
         */
      //  public static final Uri CONTENT_URI =Uri.parse("content://com.android.superdeskclock/alarm");
    	public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/specialalarms"); 

        /**
         * Year 
         * <P>Type: INTEGER</P>
         */
        public static final String YEAR = "year";
        
        /**
         * Month .
         * <P>Type: INTEGER</P>
         */
        public static final String MONTH = "month";
        
        /**
         * DayofMonth .
         * <P>Type: INTEGER</P>
         */
        public static final String DAY = "day";

        /**
         * Hour in 24-hour localtime 0 - 23.
         * <P>Type: INTEGER</P>
         */
        public static final String HOUR = "hour";
        
        /**
         * Minutes in localtime 0 - 59
         * <P>Type: INTEGER</P>
         */
        public static final String MINUTES = "minutes";

        /**
         * Alarm time in UTC milliseconds from the epoch.
         * <P>Type: INTEGER</P>
         */
        public static final String ALARM_TIME = "alarmtime";

        /**
         * True if alarm is active
         * <P>Type: BOOLEAN</P>
         */
        public static final String ENABLED = "enabled";

        /**
         * True if alarm should vibrate
         * <P>Type: BOOLEAN</P>
         */
        public static final String VIBRATE = "vibrate";

        /**
         * Message to show when alarm triggers
         * Note: not currently used
         * <P>Type: STRING</P>
         */
        public static final String MESSAGE = "message";

        /**
         * The Location of the Recoding
         * Note: 
         * <P>Type: STRING</P>
         */
        public static final String LOCATION = "location";
        
        /**
         * Audio alert to play when alarm triggers
         * <P>Type: STRING</P>
         */
        public static final String ALERT = "alert";
        
        public static final String TIMES = "times";
        
        public static final String INTERVAL = "interval";

        /**
         * The default sort order for this table
         */
        public static final String DEFAULT_SORT_ORDER =
                HOUR + ", " + MINUTES + " ASC";

        // Used when filtering enabled alarms.
        public static final String WHERE_ENABLED = ENABLED + "=1";

        static final String[] ALARM_QUERY_COLUMNS =new String[]{
        	Columns._ID,
        	Columns.YEAR,
        	Columns.MONTH,
        	Columns.DAY,
        	Columns.HOUR, 
        	Columns.MINUTES, 
        	Columns.ALARM_TIME,
        	Columns.ENABLED, 
        	Columns.VIBRATE, 
        	Columns.MESSAGE, 
        	Columns.ALERT,
        	Columns.TIMES,
        	Columns.INTERVAL,
        	Columns.LOCATION,
        	};

        /**
         * These save calls to cursor.getColumnIndexOrThrow()
         * THEY MUST BE KEPT IN SYNC WITH ABOVE QUERY COLUMNS
         */
        public static final int ALARM_ID_INDEX = 0;
        public static final int ALARM_YEAR_INDEX = 1;
        public static final int ALARM_MONTH_INDEX = 2;
        public static final int ALARM_DAY_INDEX = 3;
        public static final int ALARM_HOUR_INDEX = 4;
        public static final int ALARM_MINUTES_INDEX = 5;
        public static final int ALARM_TIME_INDEX = 6;
        public static final int ALARM_ENABLED_INDEX = 7;
        public static final int ALARM_VIBRATE_INDEX = 8;
        public static final int ALARM_MESSAGE_INDEX = 9;
        public static final int ALARM_ALERT_INDEX = 10;
        public static final int ALARM_TIMES_INDEX = 11;
        public static final int ALARM_INTERVAL_INDEX = 12;
        public static final int AlARM_LOCATION_INDEX = 13;
    }
    //////////////////////////////
    // End column definitions
    //////////////////////////////

    // Public fields
    public int        id;
    public int       year;
    public int      month;
    public int      day;
    public int        hour;
    public int        minutes;
    public long       time;
    public boolean    vibrate;
    public String     label;
    public Uri        alert;
    public boolean    enabled;
    public boolean    silent;
    public int times;
    public int interval;
    public String location;
    
    public SpecialAlarm(Cursor c) {
        id = c.getInt(Columns.ALARM_ID_INDEX);
        enabled = c.getInt(Columns.ALARM_ENABLED_INDEX) == 1;
        year = c.getInt(Columns.ALARM_YEAR_INDEX);
        month = c.getInt(Columns.ALARM_MONTH_INDEX);
        day = c.getInt(Columns.ALARM_DAY_INDEX);
        hour = c.getInt(Columns.ALARM_HOUR_INDEX);
        minutes = c.getInt(Columns.ALARM_MINUTES_INDEX);
        time = c.getLong(Columns.ALARM_TIME_INDEX);
        vibrate = c.getInt(Columns.ALARM_VIBRATE_INDEX) == 1;
        label = c.getString(Columns.ALARM_MESSAGE_INDEX);
        times = c.getInt(Columns.ALARM_TIMES_INDEX);
        interval = c.getInt(Columns.ALARM_INTERVAL_INDEX);
        location = c.getString(Columns.AlARM_LOCATION_INDEX);
        String alertString = c.getString(Columns.ALARM_ALERT_INDEX);
       
        if (SpecialAlarms.SPECIALALARM_ALERT_SILENT.equals(alertString)) {
        
            silent = true;
        } else {
            if (alertString != null && alertString.length() != 0) {
                alert = Uri.parse(alertString);
            }
  
       
            // If the database alert is null or it failed to parse, use the
            // default alert.
            if (alert == null) {
                alert = RingtoneManager.getDefaultUri(
                        RingtoneManager.TYPE_ALARM);
            }
        }
    }

    public SpecialAlarm(Parcel p) {
        id = p.readInt();
        enabled = p.readInt() == 1;
        year = p.readInt();
        month = p.readInt();
        day  = p.readInt();
        hour = p.readInt();
        minutes = p.readInt();
        time = p.readLong();
        vibrate = p.readInt() == 1;
        label = p.readString();
        alert = (Uri) p.readParcelable(null);
        silent = p.readInt() == 1;
        times=p.readInt();
        interval=p.readInt();
        location = p.readString();
    }

    // Creates a default alarm at the current time.
    public SpecialAlarm() {
        id = -1;
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(System.currentTimeMillis());
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day  = c.get(Calendar.DAY_OF_MONTH);
        hour = c.get(Calendar.HOUR_OF_DAY);
        minutes = c.get(Calendar.MINUTE);
        vibrate = true;
        alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        times=10;
        interval=0;
        location =null;
    }
    
    public SpecialAlarm(String slocation,Calendar c) {
        id = -1;
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day  = c.get(Calendar.DAY_OF_MONTH);
        hour = c.get(Calendar.HOUR_OF_DAY);
        minutes = c.get(Calendar.MINUTE);
        vibrate = true;
        alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        times=10;
        interval=0;
        location =slocation;
    }
    

    public String getLabelOrDefault(Context context) {
        if (label == null || label.length() == 0) {
            return context.getString(R.string.default_label);
        }
        return label;
    }

    /*

     */
    //如何表述一个日期：XX年XX月XX日
    static final class DaysOfMonthOfYear {

        // Bitmask of all repeating days
        private int mDays;

        DaysOfMonthOfYear(int days) {
            mDays = days;
        }

      
    }
}
